<?php




namespace app\api\controller;

use app\admin\model\User as PersonalModel;

class User extends Controller
{
    public function index()
    {
        session_start();
        $name = $_SESSION['n'] ;
        $model = new PersonalModel();
        $users = $model->getList($name);
        $this->assign('users', $users);
        return $this->renderSuccess($users, '获取数据成功');
    }
    public function userpic(){
        $name = $_SESSION['n'] ;
        $model = new PersonalModel();
        $users = $model->getList($name);
        $this->assign('users', $users);
        return $this->fetch();
    }
//    public function moduser($no)
//    {//修改用户
//        //1、取出用户信息
//        //echo $no;
//        $name = $_SESSION['n'] ;
//        $model = new PersonalModel();
//        $user = $model->getUser($no);
//        //var_dump($user);
//        $users = $model->getList($name);
//        $this->assign('users', $users);
//        //2、用户数据输出
//        return $this->fetch('moduser', ['user' => $user]);
//    }

    public function domoduser()
    {
        //1、接收参数
        $data = $this->request->param();
       // var_dump($data);
        //  2、调用模型
        $model = new PersonalModel();
        $res = $model->updateUser($data);
        //var_dump($data);
        //3、返回结果
        if ($res) {
          //  $this->success('', 'personal/user');
          // 　$this->redirect("Index/list");
            return $this->renderSuccess([], '获取数据成功');

        } else {
            return $this->renderError('修改失败');
//            $this->redirect('personal/moduser');
        }

    }
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                session_start();
                $head=$info->getSaveName();
                $name = $_SESSION['n'] ;
                $model = new PersonalModel();
                $user = $model->getHead($name,$head);
                //var_dump($user);
                //2、用户数据输出
                $users = $model->getList($name);
                $this->assign('users', $users);
                return $this->renderSuccess($users, '上传图片成功');
            }else{
                // 上传失败获取错误信息
                return $this->renderError('上传失败');
            }
        }
    }
}
